﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using MVCWeb.Models;
using System.Configuration;

namespace MVCWeb.Controllers
{
    [HandleError]
    public class HomeController : BaseController
    {
        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult Index()
        {

            
        
            ViewData["Title"] = "Home Page";
            ViewData["Message"] = "Welcome to " + System.Configuration.ConfigurationManager.AppSettings["SiteName2"] + ".";
            if (System.Configuration.ConfigurationManager.AppSettings["SiteName1"] == "Leisure Life Management")
                ViewData["DisplayWelcome"] = "<img src='../../Images/Welcome.jpg' />";            
            else if (System.Configuration.ConfigurationManager.AppSettings["SiteName1"] == "Lighthouse Management")
                ViewData["DisplayWelcome"] = "<img src='../../Images/Lighthouse_Welcome.jpg' />";            
            return View();
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult About()
        {
            ViewData["CurrentView"] = "About " + System.Configuration.ConfigurationManager.AppSettings["SiteName2"];
            return View();
        }


        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult History()
        {
            ViewData["CurrentView"] = "History of " + System.Configuration.ConfigurationManager.AppSettings["SiteName2"];
            return View();
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult Staffs()
        {
            ViewData["CurrentView"] = "Our Staffs &amp Company Directory";
            try
            {
                return View(dc.Contents.First(x => x.ContentType == "STAFF"));
            }
            catch (Exception)
            {
                return View();
            }
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult EditStaffs()
        {
            int companyId = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]);
            ViewData["CurrentView"] = "Our Staffs &amp Company Directory";
            return View(dc.Contents.Where(x => x.CompanyId == companyId && x.ContentType == "STAFF").OrderByDescending(x=>x.ID).First());
        }

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators"), ValidateInput(false)]
        public ActionResult EditStaffs(FormCollection collection)
        {
            ViewData["CurrentView"] = "Our Staffs &amp Company Directory";

            int companyId = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]);
            Content content = dc.Contents.Where(x => x.CompanyId == companyId && x.ContentType == "STAFF").OrderByDescending(x => x.ID).First();
            content.Title = "Our Staffs & Company Directory";
            content.CommunityId = null;
            content.CompanyId = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]);
            content.Body = HttpUtility.HtmlEncode(collection["Body"]);
            content.ContentType = "STAFF";
            content.Active = true;

            dc.SubmitChanges();
            return RedirectToAction("Staffs");
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult Feedback()
        {
            ViewData["CurrentView"] = "Let Us Know How We Are Doing ...";
            return View();
        }


        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult Vendor()
        {
            ViewData["CurrentView"] = "Vendor Resources";
            return View();
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult Ads()
        {
            ViewData["CurrentView"] = "Our Associate Vendors";
            return View();
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult Client()
        {
            ViewData["CurrentView"] = "Log In Your Community";
            return View();
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult UsefulLink()
        {
            ViewData["CurrentView"] = "Useful Links";
            return View(dc.Menus.Where(m => m.Type.ToLower().Equals("usefullink")));
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 10, Order = 2)]
        public ActionResult News()
        {
            ViewData["CurrentView"] = System.Configuration.ConfigurationManager.AppSettings["SiteName2"] + "'s News";
            int companyId = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]);
            return View(this.dc.News.Where(n => (n.CommunityId == 0 || n.CommunityId.HasValue == false) && (n.Active == true && n.CompanyId == companyId)).OrderByDescending(n => n.PublishedDate).ToList<News>());
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult CustomerService()
        {
            ViewData["CurrentView"] = "Customer Services";
            return View();
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult Join()
        {
            ViewData["CurrentView"] = "Let " + ConfigurationManager.AppSettings["SiteName2"] + " Manage Your Community";
            return View();
        }
        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult JoinLeisureLife()
        {
            ViewData["CurrentView"] = "Let " + ConfigurationManager.AppSettings["SiteName2"] + " Manage Your Community";
            return View();
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 60, Order = 2)]
        public ActionResult ServiceOfferred()
        {
            ViewData["CurrentView"] = "Services Offerred";
            return View();
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult FAQs()
        {
            ViewData["CurrentView"] = "FAQ's";
            return View();
        }

        [CompressFilter(Order = 1)]
        //[CacheFilter(Duration = 3600, Order = 2)]
        public ActionResult LegislativeUpdates()
        {
            ViewData["CurrentView"] = "Legislative Updates";
            return View(this.dc.Docs.Where(d => d.DocCategoryId == 1).OrderBy(d => d.Name).ToList<Doc>());
        }
        public ActionResult ClearCache()
        {
            Session.Clear();
            ViewData["CurrentView"] = "Clear Server Cache";
            return View();
        }

        [Authorize]
        public ActionResult UpdateHomeOwnerInfor()
        {
            // BEGIN: GET Community Id
            int communityId = 0;
            try
            {
                Community community;
                User user = new User();
                try
                {
                    MembershipUser membershipUser = Membership.GetUser();
                    Guid membershipId = new Guid(membershipUser.ProviderUserKey.ToString());
                    user = dc.Users.Where(u => u.MembershipId == membershipId).First();
                    community = dc.Communities.First(c => c.UserId == user.UserId || c.ManagerId == user.UserId);
                    communityId = community.CommunityId;
                }
                catch
                {
                    UsersInCommunity userInCommunity = dc.UsersInCommunities.First(x => x.UserId == user.UserId);
                    community = dc.Communities.First(x => x.CommunityId == userInCommunity.CommunityId);
                    communityId = community.CommunityId;
                }
            }
            catch { }
            ViewData["CommunityId"] = communityId;
            // END: GET Community Id

            ViewData["CurrentView"] = "Update Home Owner Infor";
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post), Authorize, ValidateInput(false)]
        public ActionResult UpdateHomeOwnerInfor(FormCollection collection)
        {
            // BEGIN: GET Community Id
            int communityId = 0;
            try
            {
                Community community;
                User user = new User();
                try
                {
                    MembershipUser membershipUser = Membership.GetUser();
                    Guid membershipId = new Guid(membershipUser.ProviderUserKey.ToString());
                    user = dc.Users.Where(u => u.MembershipId == membershipId).First();
                    community = dc.Communities.First(c => c.UserId == user.UserId || c.ManagerId == user.UserId);
                    communityId = community.CommunityId;
                }
                catch
                {
                    UsersInCommunity userInCommunity = dc.UsersInCommunities.First(x => x.UserId == user.UserId);
                    community = dc.Communities.First(x => x.CommunityId == userInCommunity.CommunityId);
                    communityId = community.CommunityId;
                }
            }
            catch { }
            ViewData["CommunityId"] = communityId;
            // END: GET Community Id

            // Update to database
            HomeOwnerInformation item = new HomeOwnerInformation();

            item.CompanyId = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]);
            item.CommunityId = communityId;
            item.FullName = collection["FullName"];
            item.Address = collection["Address"];
            item.LotNumber = collection["LotNumber"];
            item.Phone = collection["Phone"];
            item.Email = collection["Email"];
            item.Comment = HttpUtility.HtmlEncode(collection["Comment"]);
            dc.HomeOwnerInformations.InsertOnSubmit(item);
            dc.SubmitChanges();

            return RedirectToAction("UpdateHomeOwnerInforCompleted", "Home");
        }

        public ActionResult UpdateHomeOwnerInforCompleted()
        {
            ViewData["CurrentView"] = "Update Home Owner Infor Completed!";
            return View();
        }

        [Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult UpdateHomeOwnerInforManager()
        {
            if (Session["CommunityId"] == null)
            {
                return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
            }

            int communityId = Convert.ToInt32(Session["CommunityId"]);
            ViewData["CommunityId"] = communityId;
            int companyId = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]);

            return View(dc.HomeOwnerInformations.Where(x => x.CompanyId == companyId).ToList());
        }

        [Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult UpdateHomeOwnerInforDetails(int id)
        {
            if (Session["CommunityId"] == null)
            {
                return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
            }

            int communityId = Convert.ToInt32(Session["CommunityId"]);
            ViewData["CommunityId"] = communityId;

            return View(dc.HomeOwnerInformations.First(x => x.Id == id));
        }
    }
}
